package com.it2297.view;

import java.awt.BorderLayout;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;

import com.it2297.component.AbstractPagination;
import com.it2297.controller.ProductController;
import com.it2297.controller.UsedItemController;
import com.it2297.controller.WorkshopController;
import com.it2297.controller.WorkshopParticipatedController;
import com.it2297.main.UserApp;
import com.it2297.model.Workshop;
import com.it2297.model.WorkshopParticipant;
import com.it2297.renderer.WorkshopCellRenderer;
import javax.swing.JScrollPane;

public class WorkshopView extends JPanel {

	private final WorkshopController controller = new WorkshopController(
			WorkshopView.this);
	private final JTable table = new JTable();
	private final DefaultTableModel tableModel = new DefaultTableModel(0, 2) {
		@Override
		public boolean isCellEditable(int row, int column) {
			return false;
		}
	};
	private final WorkshopCellRenderer tableCellRenderer = new WorkshopCellRenderer();
	private final AbstractPagination pagination = new ViewPagination();
	private final Workshop workshop=new Workshop();
	private final WorkshopParticipant workshopparticipant=new WorkshopParticipant();
	private ProductCommentView comView = new ProductCommentView();
	public WorkshopView() {
		initGUI();
	}

	private void initGUI() {
		this.setBounds(100, 100, 750, 570);
		this.setLayout(new BorderLayout(0, 0));
		this.add(table, BorderLayout.CENTER);
		this.add(pagination, BorderLayout.SOUTH);

		table.setRowHeight(150);
		table.setShowGrid(false);
		table.setFocusable(false);
		table.setModel(tableModel);

		table.getColumnModel().getColumn(0).setMaxWidth(500);
		table.getColumnModel().getColumn(1).setMaxWidth(400);
		
		table.getColumnModel().getColumn(0).setCellRenderer(tableCellRenderer);
		table.getColumnModel().getColumn(1).setCellRenderer(tableCellRenderer);
		
	

		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent mouseEvent) {
				
			}
		});
		
		
	}

	public int getSelectedItem() {
		return (int) table.getValueAt(table.getSelectedRow(),
				table.getSelectedColumn());
	}

	public Object[] getSelectedItems() {
		ArrayList<Object> items = new ArrayList<Object>();
		for (int i : table.getSelectedRows()) {
			items.add(table.getValueAt(i, table.getSelectedColumn()));
		}
		return items.toArray();
	}

	private class ViewPagination extends AbstractPagination {
		protected ViewPagination() {
			this.setCount(3);
			this.setTotal(controller.getWorkshopRecordNumber());
		}

		@Override
		protected void action(Object[] data) {
			// clear all row
			tableModel.setRowCount(0);

			// add new row base on data size
			for (int i = 0; i < data.length; i++) {
				tableModel.addRow(new Object[] { data[i], data[i] });
			}
		}

		@Override
		protected Object[] subData(int position, int count) {
			return controller.getMultipleWorkshop(position, count).toArray();
		}
	}

	{

	}

	public void addProductListener(MouseListener productcellRenderer) {
		table.addMouseListener(productcellRenderer);
	}

	public Workshop getSelected() {
		return (Workshop) table.getModel().getValueAt(table.getSelectedRow(),
				table.getSelectedColumn());
	}
	
	public void refresh(){
		pagination.refresh();
	}
	
}
